
//Eli Elder
//mine
//makefile needs this
//cardtest1: cardtest1.c dominion.o rngs.o
//	gss -o cardtest1 -g cardtest1.c dominion.o rngs.o $(CFLAGS)


#include "dominion.h"
#include "dominion_helpers.h"
#include "interface.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h> 
#include <assert.h>
#include <math.h>
#include "rngs.h"

//set PRINTOUT to 0 to remove printfs out
#define PRINTOUT 1
#define ASSERTIVE 0

int checkMine(int card, int choice1, int choice2, int choice3, struct gameState *afterMineState, int handPos, int *bonus){
    struct gameState beforeMineState;
    int returnVal;
    int trashCard;
    int i;
    char cardName[30];
    char cardName2[30];
    int player = whoseTurn(afterMineState);
    //copy so we can test if the mine we can check
    memcpy(&beforeMineState, afterMineState, sizeof(struct gameState));
    //do the mine function card
    returnVal = cardEffect(mine, choice1, choice2, handPos, afterMineState, handPos, 0 );
    
    trashCard = beforeMineState.hand[player][choice1];
    
#if (PRINTOUT == 1)
    //inform the user if choice 1 is too high or too low
    cardNumToName(trashCard, cardName);
    if(trashCard < copper){
	printf("%s is less than copper, can only trash a treasure\n", cardName);
    }else if(trashCard > gold){
	printf("%s is more than gold, can only trash a treasure\n", cardName);
    }
    
    //inform if choice2 is too high or too low
    cardNumToName(choice2, cardName);
    if(choice2 < copper){
	printf("%s is less than copper, not allowed, please select a treasure\n", cardName);
    }else if(choice2 > gold){
	printf("%s is more than gold, not allowed please select a treasure\n", cardName);
    }
    
    cardNumToName(choice1, cardName);
    cardNumToName(choice2, cardName2);
    //inform user if cost is not enough (was wrong in code!)
    if((getCost(beforeMineState.hand[player][choice1]) + 3) < getCost(choice2)){
	//add card name
	printf("the cost of the choice1 (%s) + 3 , which is %d is less than choice2 (%s), at %d you cant get that\n", cardName, (getCost(beforeMineState.hand[player][choice1]) + 3), cardName2, getCost(choice2));
    } 
#endif	
    
    //the before gains a card
    gainCard(choice2, &beforeMineState, 2, player);
    
    //we discard the handPos card
    discardCard(handPos, player, &beforeMineState, 0);
    //then we discard the card that is to be trashed
    for(i = 0; i < beforeMineState.handCount[player]; i++){
	if(beforeMineState.hand[player][i] == trashCard){
	    discardCard(i, player, &beforeMineState, 0);
	    break;
	}
    }
    
#if (PRINTOUT == 1)
    //if the full deck counts arent the same gain and discard messed up
    if(fullDeckCount(player, 0, &beforeMineState) < fullDeckCount(player, 0, afterMineState)){
	printf("The mine card has more cards than the test\n");
    } else if(fullDeckCount(player, 0, &beforeMineState) > fullDeckCount(player, 0, afterMineState)){
	printf("The mine card has less cards than the test\n");
    }
    
#endif
    
#if (ASSERTIVE == 1)
    //this will assert if something went wrong, those ifs above tell us what went wrong
    assert(returnVal == 0);
    //assert that the after MineCard state is the same as we expect it to be with the "before" that we made like the after
    assert(memcmp(&beforeMineState, afterMineState, sizeof(struct gameState)) == 0);
#endif
    
    return 0;
}


int main(){
    int i;
    int player = 1;
    int numDeck = 5;
    int numDiscard = 5;
    int numHand = 5;
    int initialized;
    int coppers[MAX_DECK];
    int bonus = 0;
    struct gameState theGameState;
    int kingCards[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, salvager};

    for(i = 0; i < MAX_DECK; i++){
	coppers[i] = copper;
    }


    //setup and initialize the state (should do in random?)                                                                                                                
    memset(&theGameState, 23, sizeof(struct gameState));
    initialized = initializeGame(player, kingCards, 1, &theGameState);

    //assign the deck, hand, and discard                                                                                                                                   
    theGameState.discardCount[player] = numDiscard;
    theGameState.handCount[player] = numHand;
    theGameState.deckCount[player] = numDeck;

    //mem set the deck hand and discard                                                                                                                                    
    memcpy(theGameState.discard[player], coppers, sizeof(int) * MAX_DECK);
    memcpy(theGameState.hand[player], coppers, sizeof(int) * MAX_HAND);
    memcpy(theGameState.deck[player], coppers, sizeof(int) * MAX_HAND);

    theGameState.whoseTurn = player;

    //check the mine
    printf("Trying to mine a silver from a copper\n");
    theGameState.playedCards[1] = 1;
    theGameState.playedCardCount=0;
    checkMine(mine, copper, silver, 0, &theGameState, copper, &bonus);

    printf("Trying to mine a gold from a silver\n");
    theGameState.playedCards[1] = 1;
    theGameState.playedCardCount=0;
    theGameState.hand[player][silver] = silver;
    checkMine(mine, silver, gold, 0, &theGameState, silver, &bonus);
    
    printf("Trying to mine a gold from a copper\n");
    theGameState.playedCards[1] = 1;
    theGameState.playedCardCount=0;
    theGameState.hand[player][silver] = silver;
    checkMine(mine, copper, gold, 0, &theGameState, silver, &bonus);
    
    printf("Trying to mine a gardens from a silver\n");
    theGameState.playedCards[1] = 1;
    theGameState.playedCardCount=0;
    theGameState.hand[player][silver] = silver;
    checkMine(mine, silver, gardens, 0, &theGameState, silver, &bonus);

    printf("Trying to mine a feast from a silver\n");
    theGameState.playedCards[1] = 1;
    theGameState.playedCardCount=0;
    theGameState.hand[player][silver] = silver;
    checkMine(mine, silver, feast, 0, &theGameState, silver, &bonus);
    
    
    return 0;
}
